local skel = fk.CreateSkill {
  name = "emo__qingzhu",
}

Fk:loadTranslationTable{
  ["emo__qingzhu"] = "请诛",
  [":emo__qingzhu"] = "出牌阶段限一次，你可以令一名其他角色选一项：1.失去一点体力；2.将所有手牌交给你，你失去一点体力；"..
  "<br>你以此法获得的牌无次数和距离限制，且本阶段结束时，若你与其均存活，你将这些牌从弃牌堆和你手牌中交给其。",

  ["#emo__qingzhu_losehp"] = "不交手牌，你失去1点体力",
  ["#emo__qingzhu_give"] = "交出所有手牌，令其失去体力",
  ["#emo__qingzhu-choice"] = "请诛：是否将所有手牌交给 %src（本阶段结束还给你）",
  ["@@emo__qingzhu-phase-inhand"] = "请诛",
  ["#emo__qingzhu-prompt"] = "你可以令一名其他角色选择:失去体力，或将所有手牌交给你(本阶段结束时还回)",
}

skel:addEffect("active", {
  anim_type = "offensive",
  prompt = "#emo__qingzhu-prompt",
  times = function (self, player)
    return 1 - player:usedSkillTimes(self.name, Player.HistoryPhase)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < 1
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 1,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    local choice = "#emo__qingzhu_losehp"
    if not to:isKongcheng() then
      choice = room:askToChoice(to,  {
        choices = {"#emo__qingzhu_give", "#emo__qingzhu_losehp"}, skill_name = skel.name, prompt = "#emo__qingzhu-choice:"..player.id
      })
    end
    if choice:startsWith("#emo__qingzhu_losehp") then
      room:loseHp(to, 1, skel.name)
    else
      local ids = to:getCardIds("h")
      -- 考虑一下多次发动此技能
      room:addTableMark(player, "emo__qingzhu-phase", {to.id, ids})
      room:moveCardTo(to:getCardIds("h"), Player.Hand, player, fk.ReasonGive, skel.name, nil, false, to, "@@emo__qingzhu-phase-inhand")
      if not player.dead then
        room:loseHp(player, 1, skel.name)
      end
    end
  end,
})

skel:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    if not card then return false end
    local cards = Card:getIdList(card)
    return #cards > 0 and table.every(cards, function (id)
      return Fk:getCardById(id):getMark("@@emo__qingzhu-phase-inhand") ~= 0
    end)
  end,
  bypass_times = function (self, player, skill, scope, card, to)
    if not card then return false end
    local cards = Card:getIdList(card)
    return #cards > 0 and table.every(cards, function (id)
      return Fk:getCardById(id):getMark("@@emo__qingzhu-phase-inhand") ~= 0
    end)
  end,
})

skel:addEffect(fk.EventPhaseEnd, {
  is_delay_effect = true,
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player.phase == Player.Play and target == player and player:getMark("emo__qingzhu-phase") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("emo__qingzhu-phase")
    for _, dat in ipairs(mark) do
      local to = room:getPlayerById(dat[1])
      local ids = dat[2]
      if not to.dead then
        local hand, pile = player:getCardIds("h"), room.discard_pile
        local give = table.filter(ids, function (id)
          return table.contains(hand, id) or table.contains(pile, id)
        end)
        if #give > 0 then
          room:moveCardTo(give, Player.Hand, to, fk.ReasonGive, skel.name, nil, false, player)
        end
      end
    end
  end,
})


return skel
